package com.fingard.dsp.bank.directbank.vbao03;

import com.alibaba.fastjson.JSONObject;
import com.fingard.dsp.bank.directConfig.ActSetItem;
import com.fingard.dsp.bank.directbank.FailCode;
import com.fingard.dsp.bank.directbank.PaymentState;
import com.fingard.dsp.bank.directbank.bankInterface.IRealTime;
import com.fingard.dsp.bank.directbank.vbao03.request.PayQueryDataForm;
import com.fingard.dsp.bank.directbank.vbao03.request.PayRequest;
import com.fingard.dsp.bank.reqrespobj.single.ReqQrySingle;
import com.fingard.dsp.bank.reqrespobj.single.ReqSingle;
import com.fingard.dsp.bank.reqrespobj.single.RetAtsSingle;
import com.fingard.dsp.bank.reqrespobj.single.detail.RetAtsSglDetail;
import com.fingard.text.StringHelper;

public class VBAO03RealTime extends VBAO03Base implements IRealTime {
    @Override
    public RetAtsSingle d_9108_RealTimeRec(ReqSingle p_req9108) throws Exception {
        RetAtsSingle retAts = new RetAtsSingle(p_req9108.reqHeader);
        RetAtsSglDetail tmpRetDetail = new RetAtsSglDetail();
        tmpRetDetail.reqSeqID = p_req9108.singleDetail.askNumber;

        try {
            ActSetItem actItem = getBankConfig().getActSetItem(p_req9108.singleDetail.recAct, "");
            if (actItem == null) {
                tmpRetDetail.respCode = "0";
                tmpRetDetail.respInfo = "未找到账号:[" + p_req9108.singleDetail.recAct + "]商户配置信息!";
                tmpRetDetail.reqSeqID = p_req9108.singleDetail.askNumber;
                tmpRetDetail.transState = PaymentState.FAIL_Payment;
                tmpRetDetail.payInfoCode = FailCode.E9981;
                retAts.writeRetDetail(tmpRetDetail);
                return retAts;
            }

            PayRequest payRequest = new PayRequest();
            payRequest.setOrderNo(p_req9108.singleDetail.askNumber);
            String bankCode = getBankConfig().getMapValueByKey("BankCodeMapping", p_req9108.singleDetail.payBankCode);
            if (StringHelper.isNullOrEmpty(bankCode)) {
                retAts.writeRespCode("0");
                retAts.writeRespInfo("未找到BankCodeMapping中对应" + p_req9108.singleDetail.payBankCode + "配置信息,请检查config.xml相关配置!");
                retAts.writeEnd();
                return retAts;
            }
            payRequest.setChannelNo(bankCode);
            payRequest.setBankCode(bankCode);
            payRequest.setCardName(p_req9108.singleDetail.payName);
            payRequest.setCardNo(p_req9108.singleDetail.payAct);
            if ("1".equals(p_req9108.singleDetail.isPrivate)){
                payRequest.setAccountType("private");
            } else {
                payRequest.setAccountType("public");
            }
            if ("1".equals(p_req9108.singleDetail.cardType)) {
                payRequest.setCardType("book");
            } else if ("2".equals(p_req9108.singleDetail.cardType)) {
                payRequest.setCardType("debit");
            } else if ("3".equals(p_req9108.singleDetail.cardType)) {
                payRequest.setCardType("credit");
            }
            payRequest.setMobile(p_req9108.singleDetail.cellPhone);
            payRequest.setCertNo(p_req9108.singleDetail.certNum);
            payRequest.setCertType(convertCertTypeToBank(p_req9108.singleDetail.certType));
            payRequest.setAmount(Long.parseLong(StringHelper.amountMultiply100(p_req9108.singleDetail.payAmount)));
            payRequest.setCurrency("CNY");
            payRequest.setSubject("");
            payRequest.setRemarks("");

            JSONObject jsonObjectReturn = sendToBank(JSONObject.toJSONString(payRequest), actItem, bankFront.getUrl("singleRecPay"));

            if (!"".equals(jsonObjectReturn) && jsonObjectReturn != null) {
                JSONObject respBizContent = decryptByRSA(jsonObjectReturn, actItem);
                String respCode = respBizContent.getString("returnCode");
                String tmpRespInfo = respBizContent.getString("returnMsg");
                if (respCode != null) {
                    tmpRetDetail.respCode = "0";
                    tmpRetDetail.transAmt = respBizContent.getString("amount");
                    tmpRetDetail.respInfo = tmpRespInfo;
                    tmpRetDetail.transState = respBizContent.getString("orderStatus");
                    tmpRetDetail.payInfoCode = respCode;
                } else {
                    tmpRetDetail.respCode = "-1";
                    tmpRetDetail.respInfo = tmpRespInfo;
                }
            } else {
                tmpRetDetail.respCode = "-1";
                tmpRetDetail.respInfo = "银行响应内容为空,请联系银行咨询!";
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            tmpRetDetail.respCode = "-1";
            tmpRetDetail.respInfo = ex.getMessage();
            WriteBankLogLn(ex);
        }

        retAts.writeRetDetail(tmpRetDetail);
        return retAts;
    }

    @Override
    public RetAtsSingle d_9144_QryRealTimeRec(ReqQrySingle p_req9144) throws Exception {
        RetAtsSingle retAts = new RetAtsSingle(p_req9144.reqHeader);
        RetAtsSglDetail tmpRetDetail = new RetAtsSglDetail();
        tmpRetDetail.reqSeqID = p_req9144.askNumber;

        try {
            ActSetItem actItem = getBankConfig().getActSetItem(p_req9144.recAct, "");
            if (actItem == null) {
                tmpRetDetail.respCode = "0";
                tmpRetDetail.respInfo = "未找到账号:[" + p_req9144.recAct + "]商户配置信息!";
                tmpRetDetail.reqSeqID = p_req9144.askNumber;
                tmpRetDetail.transState = PaymentState.FAIL_Payment;
                tmpRetDetail.payInfoCode = FailCode.E9981;
                retAts.writeRetDetail(tmpRetDetail);
                return retAts;
            }

            PayQueryDataForm form = new PayQueryDataForm();
            form.setOrderNo(p_req9144.askNumber);

            JSONObject jsonObjectReturn = sendToBank(JSONObject.toJSONString(form), actItem, bankFront.getUrl("singleRecPayQry"));

            if (!"".equals(jsonObjectReturn) && jsonObjectReturn != null) {
                JSONObject respBizContent = decryptByRSA(jsonObjectReturn, actItem);
                String respCode = respBizContent.getString("returnCode");
                String tmpRespInfo = respBizContent.getString("returnMsg");
                if (respCode != null) {
                    tmpRetDetail.respCode = "0";
                    tmpRetDetail.transAmt = respBizContent.getString("amount");
                    tmpRetDetail.respInfo = tmpRespInfo;
                    tmpRetDetail.transState = respBizContent.getString("orderStatus");
                    tmpRetDetail.payInfoCode = respCode;
                } else {
                    tmpRetDetail.respCode = "-1";
                    tmpRetDetail.respInfo = tmpRespInfo;
                }
            } else {
                tmpRetDetail.respCode = "-1";
                tmpRetDetail.respInfo = "银行响应内容为空,请联系银行咨询!";
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            tmpRetDetail.respCode = "-1";
            tmpRetDetail.respInfo = ex.getMessage();
            WriteBankLogLn(ex);
        }

        retAts.writeRetDetail(tmpRetDetail);
        return retAts;
    }
}
